
import 'package:fate_blind_box/network/Api.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/utils/snack_bar.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fate_blind_box/widget/date_picker/flutter_datetime_picker.dart';

class BlindFormUi extends StatelessWidget {
  const BlindFormUi({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const _BlindFormWidget();
  }
}

class _BlindFormWidget extends StatefulWidget {
  const _BlindFormWidget({Key? key}) : super(key: key);

  @override
  State<_BlindFormWidget> createState() => _BlindFormWidgetState();
}

class _BlindFormWidgetState extends State<_BlindFormWidget> {

  final _formData = <String, dynamic>{};
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: buildAppBar(),
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 12),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              buildFormFieldItem("活动开始时间", "请选择活动开始时间", "startDate", true),
              buildFormFieldItem("活动结束时间", "请选择活动结束时间", "endDate", true),
              buildFormFieldItem("联系人", "请输入联系人", "contactUser", false),
              buildFormFieldItem("联系电话", "请输入联系电话", "contactPhone", false),
              buildFormFieldItem("活动费用", "请输入活动费用", "nonMemberFee", false),
              buildGroupSwitch(),
              buildTipForm()
            ],
          ),
        )
      ),
    );
  }

  AppBar buildAppBar() {
    return AppBar(
      centerTitle: true,
      backgroundColor: Colors.white,
      elevation: 0,
      title: Text(
        "相亲活动",
        style: TextStyle(
          color: ThemeColor.textColor,
          fontSize: 16
        ),
      ),
      actions: [
        GestureDetector(
          onTap: () {
            _formKey.currentState?.save();
            _submit();
          },
          child: Container(
            width: 56,
            alignment: Alignment.center,
            child: Text(
              "发表",
              style: TextStyle(
                color: ThemeColor.mainColor,
              ),
            ),
          )
        )
      ],
    );
  }

  Widget buildFormFieldItem(
      String key,
      String hint,
      String paramsKey,
      bool
      isText) {
    return Container(
      height: 56,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: ThemeColor.dividerColor,
            width: 1
          )
        )
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            key,
            style: TextStyle(
              color: ThemeColor.textColor,
              fontSize: 14
            ),
          ),
          Expanded(
            child: isText ? buildClickItem(hint, paramsKey) : buildField(hint, paramsKey)
          )
        ],
      ),
    );
  }

  Widget buildClickItem(String hint, String paramsKey) {
    return GestureDetector(
      onTap: () {
        final cur = DateTime.now();
        DatePicker.showDatePicker(context,
            showTitleActions: true,
            minTime: DateTime(cur.year, cur.month, cur.day),
            maxTime: DateTime(2099, 12, 31),
            onChanged: (date) {  },
            onConfirm: (date) {
              setState(() {
                _formData[paramsKey] = date.toString().substring(0, 19);
              });
            }, currentTime: DateTime.now(), locale: LocaleType.zh);
      },
      child: Text(
          _formData[paramsKey] == null || _formData[paramsKey].toString().isEmpty ? hint : _formData[paramsKey],
        textAlign: TextAlign.right,
        style: TextStyle(
          color: _formData[paramsKey] == null || _formData[paramsKey].toString().isEmpty ? ThemeColor.textColorSecond : ThemeColor.textColor
        ),
      )
    );
  }

  Widget buildField(String hint, String paramsKey) {
    return TextFormField(
      initialValue: _formData[paramsKey],
      decoration: InputDecoration(
        hintText: hint,
        hintStyle:
        TextStyle(color: ThemeColor.textColorSecond, fontSize: 14),
        enabledBorder: const UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent)),
        focusedBorder: const UnderlineInputBorder(
            borderSide: BorderSide(color: Colors.transparent)),
      ),
      textAlign: TextAlign.right,
      onSaved: (value) {
        if (value != null && value.isNotEmpty) {
          _formData[paramsKey] = value;
        }
      },
    );
  }

  var switchGroup = false;
  Widget buildGroupSwitch() {
    return SizedBox(
      height: 56,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Expanded(child: Text(
            "拼团相亲",
            style: TextStyle(
                color: ThemeColor.textColor,
                fontSize: 14
            ),
          )),
          CupertinoSwitch(
            activeColor: ThemeColor.mainColor,
            value: switchGroup,
            onChanged: (e) {
              switchGroup = e;
              setState(() {});
            }
          )
        ],
      ),
    );
  }

  Widget buildTipForm() {
    return Container(
      padding: const EdgeInsets.fromLTRB(12, 15, 12, 6),
      decoration: ShapeDecoration(
        color: const Color(0xFFF7F7F7),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(5)
        )
      ),
      child: Column(
        children: [
          Row(
            children: [
              Expanded(child: Text("直接推荐奖励", style: TextStyle(color: ThemeColor.textColor),)),
              Text("20%", style: TextStyle(color: ThemeColor.textColor))
            ],
          ),
          const SizedBox(height: 15,),
          Row(
            children: [
              Text("间接推荐奖励", style: TextStyle(color: ThemeColor.textColor)),
              Expanded(
                child: buildField("请输入", "indirect"),
              )
            ],
          ),
        ],
      ),
    );
  }

  _submit() async {
    if (_formData.length < 6) {
      ThemeSnackBar.show(context, "请完善信息");
      return;
    }
    final res = await Api.getInstance().publishBlindActive(_formData);
    if (res['code'] == 0) {

    } else {
      ThemeSnackBar.show(context, res['msg']);
    }
  }
}

